// $Id: future.c 35 2011-04-15 12:34:19Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2010 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "log.h"
#include "reactor.h"
#include "socket.h"

#include "future.h"

struct niofuture *niofuture_new(struct nioreactor *reactor)
{
	struct niofuture *future;

	niodebug("constructing");

	if (!(future = calloc(1, sizeof(struct niofuture)))) {
		nioerror("failed to calloc nio");
		goto error;
	}

	future->reactor = reactor;

	return future;

error:
	niofuture_free(future);
	return NULL;
}

void niofuture_free(struct niofuture *future)
{
	if (!future) {
		return;
	}

	niofuture_cancel(future);

	free(future);
}

int niofuture_enable(struct niofuture *future, struct niosocket *socket,
		enum niofuture_flag flag,
		struct timeval *timeout, niofuture_cb cb, void *cbarg)
{
	int rc;

	if (!future || !cb) {
		nioerror("null argument(s)");
		rc = NIO_EINVAL;
		goto finally;
	}

	switch (flag) {
	case NIO_TIMEUP:
		niodebug("waiting for time up: %p", cb);
		break;
	case NIO_READABLE:
	case NIO_WRITABLE:
		if (!socket) {
			nioerror("null socket");
			rc = NIO_EINVAL;
			goto finally;
		}

		niodebug("waiting for socket %d %s: %p",
				socket->descriptor,
				flag == NIO_READABLE ? "readable" : "writable",
				cb);

		if (socket->descriptor == INVALID_SOCKET) {
			nioerror("socket not opened");
			rc = NIO_ECLOSED;
			goto finally;
		}
		break;
	default:
		nioerror("unexpected flag: %d", flag);
		rc = NIO_EUNKNOWN;
		goto finally;
	}

	if ((rc = nioreactor_insert(future->reactor, future, socket,
			flag, timeout, cb, cbarg))) {
		nioerror("failed to insert event");
		goto finally;
	}

	rc = 0;

finally:
	return rc;
}

int niofuture_writable(struct niofuture *future, struct niosocket *socket,
		struct timeval *timeout, niofuture_cb cb, void *cbarg)
{
	return niofuture_enable(future, socket, NIO_WRITABLE,
			timeout, cb, cbarg);
}

int niofuture_readable(struct niofuture *future, struct niosocket *socket,
		struct timeval *timeout, niofuture_cb cb, void *cbarg)
{
	return niofuture_enable(future, socket, NIO_READABLE,
			timeout, cb, cbarg);
}

int niofuture_timeup(struct niofuture *future,
		struct timeval *timeout, niofuture_cb cb, void *cbarg)
{
	return niofuture_enable(future, NULL, NIO_TIMEUP,
			timeout, cb, cbarg);
}

int niofuture_cancel(struct niofuture *future)
{
	niodebug("canceling");
	return nioreactor_remove(future->reactor, future);
}
